<?php

$config_file = $argv[3] ?? __DIR__ . '/synchro.json';
if ($argc < 2) {
    exit("Few arguments\n");
}
$work_file = $argv[1];
$sample_suffix = $argv[2] ?? '.sample';

if (!file_exists($work_file)) {
    exit("Work file not found\n");
}
if (!file_exists($config_file)) {
    exit("Config file not found\n");
}

$wf_path_info = pathinfo($work_file);
$sample_file = $wf_path_info['dirname'] . '/' . $wf_path_info['filename'] . $sample_suffix . '.' . $wf_path_info["extension"];

$replace_json = file_get_contents($config_file);
$replace_array = json_decode($replace_json, true);

if (is_null($replace_array)) {
    exit("Error json_decode\n");
}

$replaced_string = file_get_contents($work_file);
foreach ($replace_array as $key => $value) {
    $replaced_string = str_replace($key, $value, $replaced_string);
}
file_put_contents($sample_file, $replaced_string);
